package org.lmj.dos.web.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.lmj.dos.common.bean.ServerResponse;
import org.lmj.dos.common.pojo.UserEntity;
import org.lmj.dos.mapper.UserMapper;
import org.lmj.dos.web.resultdo.UsersVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

@RequestMapping
@RestController
public class UserController {
    @Autowired
    UserMapper userMapper;
    //  new ObjectMapper().readValue(userReq,Map.class);
    //HttpClientResult<ResponseEntity> response = objectMapper.readValue(responseContent, new TypeReference<HttpClientResult<ResponseEntity>>() {});

    @GetMapping("/user")
    public ServerResponse user(@RequestBody UserEntity userReq) throws IOException {
        QueryWrapper<UserEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("username",userReq.getUsername());
        wrapper.eq("password",userReq.getPassword());
        UserEntity dbUser = userMapper.selectOne(wrapper);
        if (dbUser!=null){
            return ServerResponse.success(dbUser);
        }
        return ServerResponse.fail(400,"error username or password");
    }

    @PostMapping("/user/login")
    public ServerResponse userLogin(@RequestBody UserEntity userReq) throws IOException {
        QueryWrapper<UserEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("username",userReq.getUsername());
        wrapper.eq("password",userReq.getPassword());
        UserEntity dbUser = userMapper.selectOne(wrapper);
        if (dbUser!=null){
            return ServerResponse.success(dbUser);
        }
        return ServerResponse.fail(400,"error username or password");
    }

    @PostMapping("/user/logout")
    public ServerResponse userLoginOut() throws IOException {
        return ServerResponse.success(null);
    }

}
